#!/usr/bin/python2
# -*- coding: utf-8 -*-
# Name:     settings.py
# Author:   xiooli <xioooli[at]yahoo.com.cn>
# Version:  111108

import yaml, os
from functions import dict2obj

class Settings():
    def __init__(self, settings_file):
        self.settings_file = settings_file
        if not os.path.exists(settings_file):
            self.all_settings = {}
            self.all_settings['positive_ions'] = [
                    [1.007276032, 'H+'],
                    [22.98922067, 'Na+'],
                    [38.9631579, 'K+'],
                    [18.03382513, 'NH4+'],
                    [0, 'Other']]
            self.all_settings['negative_ions'] = [
                    [-1.0083740321, '-H'],
                    [0, 'Other']]

            self.all_settings['db_file'] = 'dbs/rules.db'
            self.all_settings['iso_width'] = 0.5
            self.all_settings['noise_level'] = 5
            self.all_settings['deviation'] = deviation = 5 / 1000000.0
            self.all_settings['lang'] = 'en_US'
            self.all_settings['deactivated_plugins'] = ['']
            self.all_settings['search_engine'] = 'pubchem'
            self.all_settings['num_confirm_results'] = 10000

            self.write_settings()
        else:
            self.get_settings()

    def get_settings(self):
        f = open(self.settings_file, 'r')
        self.all_settings = yaml.load(f)
        f.close()
        return dict2obj(self.all_settings)

    def write_settings(self):
            f = open(self.settings_file, 'w')
            yaml.dump(self.all_settings, f)
            f.close()

if __name__ == '__main__':
    import sys
    s = Settings(sys.argv[1])
    print s.get_settings().iso_width
